import { createRouter, createWebHistory } from 'vue-router';
import LoginRegister from '../views/yonghu/LoginRegister.vue';

const handleLazyLoadError = (componentName) => (error) => {
  console.error(`Failed to load ${componentName} component:`, error);
  return LoginRegister;
};

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginRegister,
    meta: { title: '系统登录' }
  },
  {
    path: '/yonghu/my',
    name: 'my',
    component: () => import('@/views/yonghu/My.vue').catch(handleLazyLoadError('My')),
    meta: { title: '我的', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/hospital-info',
    name: 'HospitalInfo',
    component: () => import('@/views/yonghu/HospitalInfo.vue').catch(handleLazyLoadError('HospitalInfo')),
    meta: { title: '医院信息', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/my-appointments',
    name: 'MyAppointments',
    component: () => import('@/views/yonghu/MyAppointments.vue').catch(handleLazyLoadError('MyAppointments')),
    meta: { title: '我的预约', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/online-registration',
    name: 'OnlineRegistration',
    component: () => import('@/views/yonghu/OnlineRegistration.vue').catch(handleLazyLoadError('OnlineRegistration')),
    meta: { title: '在线挂号', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/profile',
    name: 'Profile',
    component: () => import('@/views/yonghu/Profile.vue'),
    meta: { title: '个人信息', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/profile-info',
    name: 'ProfileInfo',
    component: () => import('@/views/yonghu/ProfileInfo.vue').catch(handleLazyLoadError('ProfileInfo')),
    meta: { title: '就诊人信息', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/payment',
    name: 'Payment',
    component: () => import('@/views/yonghu/Payment.vue').catch(handleLazyLoadError('Payment')),
    meta: { title: '挂号缴费', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/doctor-search',
    name: 'DoctorSearch',
    component: () => import('@/views/yonghu/DoctorSearch.vue'),
    meta: { title: '名医搜索', requiresAuth: true, role: 'patient' }
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

export default router;